package com.fyzk.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnBasketPayTeamDeleteListener;
import com.tyjc.lottery.R;
import com.fyzk.model.JCLQMatchData;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.LotteryIDUtils;

import java.util.ArrayList;
import java.util.List;

public class JCLQSFPayAdapter extends JCLQZhiFuBaseAdapter {

    private static final String TAG = "JC_ZhiFuAdapter";

    private boolean isdanguan;
    private List<JCLQMatchData> infors;
    private LayoutInflater mInflater;
    private Handler handler;
    private int zhuShu;

    private int color_bai;
    private int color_hei;
    private int color_gray;
    private int color_zong;
    private int lotteryId;

    private OnBasketPayTeamDeleteListener listener;

    public JCLQSFPayAdapter(Context context, boolean type,
                            List<JCLQMatchData> infors, Handler handler, int lotteryId,OnBasketPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        mInflater = LayoutInflater.from(context);
        this.handler = handler;
        color_bai = Color.WHITE;
        color_hei = context.getResources().getColor(R.color.deep_black);
        color_gray = context.getResources().getColor(R.color.color_666666);
        color_zong = context.getResources().getColor(R.color.zc_bf_left);
        this.lotteryId = lotteryId;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public Object getItem(int position) {
        if (position >= infors.size()) {
            return null;
        }
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        if (position >= infors.size()) {
            return 0;
        }
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.jclq_zhifu_listview_item, null);
            holder = new ViewHolder();
            holder.zhu = convertView.findViewById(R.id.jclq_zhifu_list_item_zhu);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.delete = convertView.findViewById(R.id.jclq_zhifu_list_item_delete);
            holder.sheng = convertView.findViewById(R.id.jclq_zhifu_list_item_ll_sheng);
            holder.fu = convertView.findViewById(R.id.jclq_zhifu_list_item_ll_fu);
            holder.sheng1 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_sheng1);
            holder.fu1 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_fu1);
            holder.fen = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_fen);
            holder.sheng2 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_sheng2);
            holder.fu2 = convertView.findViewById(R.id.jclq_zhifu_list_item_tv_fu2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCLQMatchData infor = infors.get(position);
        holder.zhu.setVisibility(View.VISIBLE);
        holder.sheng1.setText(infor.getHomeTeam());
        holder.fu1.setText(infor.getAwayTeam());

        JCLQMatchData.SPInfo spInfo_s = null;
        JCLQMatchData.SPInfo spInfo_f = null;

        if (lotteryId == LotteryIDUtils.JLRSF_LOTTERY_ID_INT) {
            spInfo_s = infor.splInfos.get(infor.LETVS_3);
            spInfo_f = infor.splInfos.get(infor.LETVS_0);
            holder.fen.setVisibility(View.VISIBLE);
            holder.fen.setChecked(infor.getHandicap().contains("+"));
            holder.fen.setText("主\n" + infor.getHandicap());
        } else {
            spInfo_s = infor.splInfos.get(infor.VS_3);
            spInfo_f = infor.splInfos.get(infor.VS_0);
            holder.fen.setVisibility(View.GONE);
        }
        holder.sheng2.setText("(主)胜" + spInfo_s.spValue);
        holder.fu2.setText("(客)胜" + spInfo_f.spValue);

        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor.getMatchNum()));
        holder.sheng.setTag(holder);
        holder.fu.setTag(holder);
        holder.delete.setTag(infor);

        if (spInfo_s.isSel) {
            holder.sheng.setBackgroundResource(R.drawable.btn_bg_eb1b42_ddd_bian_redius);
            holder.sheng1.setTextColor(color_bai);
            holder.sheng2.setTextColor(color_bai);
        } else {
            holder.sheng.setBackgroundResource(R.drawable.btn_bg_dddddd_bian_redius);
            holder.sheng1.setTextColor(color_hei);
            holder.sheng2.setTextColor(color_gray);
        }
        if (spInfo_f.isSel) {
            holder.fu.setBackgroundResource(R.drawable.btn_bg_eb1b42_ddd_bian_redius);
            holder.fu1.setTextColor(color_bai);
            holder.fu2.setTextColor(color_bai);
        } else {
            holder.fu.setBackgroundResource(R.drawable.btn_bg_dddddd_bian_redius);
            holder.fu1.setTextColor(color_hei);
            holder.fu2.setTextColor(color_zong);
        }
        holder.delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                JCLQMatchData info = (JCLQMatchData) v.getTag();
                delQueue(info);
                if (listener != null) {
                    listener.onDelete(info);
                }
            }
        });
        jiSuanZhuShu();
        return convertView;
    }

    private void delQueue(JCLQMatchData info) {
        for (JCLQMatchData.SPInfo pl : info.plInfos) {
            pl.isSel = false;
        }
        info.clickNum = 0;
        infors.remove(info);
        notifyDataSetChanged();
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCLQMatchData> getInfor(List<JCLQMatchData> infors) {
        ArrayList<JCLQMatchData> list = new ArrayList<>();
        for (int i = 0; i < infors.size(); i++) {
            JCLQMatchData info = infors.get(i);
            if (info.clickNum > 0) {
                list.add(info);
            }
        }
        return list;
    }

    /**
     * 计算注数（待修改） ///苗强 ---暂不支持 多串过关 只支持 自由过关
     */
    public void jiSuanZhuShu() {

        List<Integer> clicknums = new ArrayList<Integer>();
        List<Boolean> dannums = new ArrayList<Boolean>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        boolean isDanShi = true;
        for (int i = 0; i < infors.size(); i++) {
            JCLQMatchData info = infors.get(i);
            if (info.clickNum != 0) {
                changCi++;
                if (info.clickNum != 1) {
                    isDanShi = false;
                }
            }
            clicknums.add(info.clickNum);
            dannums.add(false);// 暂不支持胆拖
        }

        List<Integer> chuans = new ArrayList<Integer>();

        // JC_GuoGuanDialog.setResult1(changCi);
        if (isdanguan) {
            for (int i = 0; i < clicknums.size(); i++) {
                zhuShu += clicknums.get(i);
            }
        } else {
            LogUtil.e("result : " + JC_GuoGuanDialog.result);
            String chuan = JC_GuoGuanDialog.result;

            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }
                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);
                String betcode = JZBetContentUtil.getContentLQSF(isdanguan, chuan, lotteryId);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);
            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }

    public List<JCLQMatchData> getInfors() {
        return infors;
    }

    public void setJLInfors(List<JCLQMatchData> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
        if(infors.size()==0){
            jiSuanZhuShu();
        }
    }

    class ViewHolder {
        LinearLayout zhu;
        LinearLayout delete;
        LinearLayout sheng;
        LinearLayout fu;
        TextView sheng1;
        TextView fu1;
        TextView sheng2;
        TextView fu2;
        CheckBox fen;
        TextView week_num;

    }

}
